<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />

<meta name="author" content="Logan Karsten" />

<meta name="date" content="2017-05-01" />

<title>Collect SNOTEL SWE from Niwot Ridge and compare directly to model output</title>



<style type="text/css">code{white-space: pre;}</style>
<style type="text/css">
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
  margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; }
code > span.dt { color: #902000; }
code > span.dv { color: #40a070; }
code > span.bn { color: #40a070; }
code > span.fl { color: #40a070; }
code > span.ch { color: #4070a0; }
code > span.st { color: #4070a0; }
code > span.co { color: #60a0b0; font-style: italic; }
code > span.ot { color: #007020; }
code > span.al { color: #ff0000; font-weight: bold; }
code > span.fu { color: #06287e; }
code > span.er { color: #ff0000; font-weight: bold; }
</style>
<style type="text/css">
  pre:not([class]) {
    background-color: white;
  }
</style>


<link href="data:text/css,body%20%7B%0A%20%20background%2Dcolor%3A%20%23fff%3B%0A%20%20margin%3A%201em%20auto%3B%0A%20%20max%2Dwidth%3A%20700px%3B%0A%20%20overflow%3A%20visible%3B%0A%20%20padding%2Dleft%3A%202em%3B%0A%20%20padding%2Dright%3A%202em%3B%0A%20%20font%2Dfamily%3A%20%22Open%20Sans%22%2C%20%22Helvetica%20Neue%22%2C%20Helvetica%2C%20Arial%2C%20sans%2Dserif%3B%0A%20%20font%2Dsize%3A%2014px%3B%0A%20%20line%2Dheight%3A%201%2E35%3B%0A%7D%0A%0A%23header%20%7B%0A%20%20text%2Dalign%3A%20center%3B%0A%7D%0A%0A%23TOC%20%7B%0A%20%20clear%3A%20both%3B%0A%20%20margin%3A%200%200%2010px%2010px%3B%0A%20%20padding%3A%204px%3B%0A%20%20width%3A%20400px%3B%0A%20%20border%3A%201px%20solid%20%23CCCCCC%3B%0A%20%20border%2Dradius%3A%205px%3B%0A%0A%20%20background%2Dcolor%3A%20%23f6f6f6%3B%0A%20%20font%2Dsize%3A%2013px%3B%0A%20%20line%2Dheight%3A%201%2E3%3B%0A%7D%0A%20%20%23TOC%20%2Etoctitle%20%7B%0A%20%20%20%20font%2Dweight%3A%20bold%3B%0A%20%20%20%20font%2Dsize%3A%2015px%3B%0A%20%20%20%20margin%2Dleft%3A%205px%3B%0A%20%20%7D%0A%0A%20%20%23TOC%20ul%20%7B%0A%20%20%20%20padding%2Dleft%3A%2040px%3B%0A%20%20%20%20margin%2Dleft%3A%20%2D1%2E5em%3B%0A%20%20%20%20margin%2Dtop%3A%205px%3B%0A%20%20%20%20margin%2Dbottom%3A%205px%3B%0A%20%20%7D%0A%20%20%23TOC%20ul%20ul%20%7B%0A%20%20%20%20margin%2Dleft%3A%20%2D2em%3B%0A%20%20%7D%0A%20%20%23TOC%20li%20%7B%0A%20%20%20%20line%2Dheight%3A%2016px%3B%0A%20%20%7D%0A%0Atable%20%7B%0A%20%20margin%3A%201em%20auto%3B%0A%20%20border%2Dwidth%3A%201px%3B%0A%20%20border%2Dcolor%3A%20%23DDDDDD%3B%0A%20%20border%2Dstyle%3A%20outset%3B%0A%20%20border%2Dcollapse%3A%20collapse%3B%0A%7D%0Atable%20th%20%7B%0A%20%20border%2Dwidth%3A%202px%3B%0A%20%20padding%3A%205px%3B%0A%20%20border%2Dstyle%3A%20inset%3B%0A%7D%0Atable%20td%20%7B%0A%20%20border%2Dwidth%3A%201px%3B%0A%20%20border%2Dstyle%3A%20inset%3B%0A%20%20line%2Dheight%3A%2018px%3B%0A%20%20padding%3A%205px%205px%3B%0A%7D%0Atable%2C%20table%20th%2C%20table%20td%20%7B%0A%20%20border%2Dleft%2Dstyle%3A%20none%3B%0A%20%20border%2Dright%2Dstyle%3A%20none%3B%0A%7D%0Atable%20thead%2C%20table%20tr%2Eeven%20%7B%0A%20%20background%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0A%0Ap%20%7B%0A%20%20margin%3A%200%2E5em%200%3B%0A%7D%0A%0Ablockquote%20%7B%0A%20%20background%2Dcolor%3A%20%23f6f6f6%3B%0A%20%20padding%3A%200%2E25em%200%2E75em%3B%0A%7D%0A%0Ahr%20%7B%0A%20%20border%2Dstyle%3A%20solid%3B%0A%20%20border%3A%20none%3B%0A%20%20border%2Dtop%3A%201px%20solid%20%23777%3B%0A%20%20margin%3A%2028px%200%3B%0A%7D%0A%0Adl%20%7B%0A%20%20margin%2Dleft%3A%200%3B%0A%7D%0A%20%20dl%20dd%20%7B%0A%20%20%20%20margin%2Dbottom%3A%2013px%3B%0A%20%20%20%20margin%2Dleft%3A%2013px%3B%0A%20%20%7D%0A%20%20dl%20dt%20%7B%0A%20%20%20%20font%2Dweight%3A%20bold%3B%0A%20%20%7D%0A%0Aul%20%7B%0A%20%20margin%2Dtop%3A%200%3B%0A%7D%0A%20%20ul%20li%20%7B%0A%20%20%20%20list%2Dstyle%3A%20circle%20outside%3B%0A%20%20%7D%0A%20%20ul%20ul%20%7B%0A%20%20%20%20margin%2Dbottom%3A%200%3B%0A%20%20%7D%0A%0Apre%2C%20code%20%7B%0A%20%20background%2Dcolor%3A%20%23f7f7f7%3B%0A%20%20border%2Dradius%3A%203px%3B%0A%20%20color%3A%20%23333%3B%0A%7D%0Apre%20%7B%0A%20%20white%2Dspace%3A%20pre%2Dwrap%3B%20%20%20%20%2F%2A%20Wrap%20long%20lines%20%2A%2F%0A%20%20border%2Dradius%3A%203px%3B%0A%20%20margin%3A%205px%200px%2010px%200px%3B%0A%20%20padding%3A%2010px%3B%0A%7D%0Apre%3Anot%28%5Bclass%5D%29%20%7B%0A%20%20background%2Dcolor%3A%20%23f7f7f7%3B%0A%7D%0A%0Acode%20%7B%0A%20%20font%2Dfamily%3A%20Consolas%2C%20Monaco%2C%20%27Courier%20New%27%2C%20monospace%3B%0A%20%20font%2Dsize%3A%2085%25%3B%0A%7D%0Ap%20%3E%20code%2C%20li%20%3E%20code%20%7B%0A%20%20padding%3A%202px%200px%3B%0A%7D%0A%0Adiv%2Efigure%20%7B%0A%20%20text%2Dalign%3A%20center%3B%0A%7D%0Aimg%20%7B%0A%20%20background%2Dcolor%3A%20%23FFFFFF%3B%0A%20%20padding%3A%202px%3B%0A%20%20border%3A%201px%20solid%20%23DDDDDD%3B%0A%20%20border%2Dradius%3A%203px%3B%0A%20%20border%3A%201px%20solid%20%23CCCCCC%3B%0A%20%20margin%3A%200%205px%3B%0A%7D%0A%0Ah1%20%7B%0A%20%20margin%2Dtop%3A%200%3B%0A%20%20font%2Dsize%3A%2035px%3B%0A%20%20line%2Dheight%3A%2040px%3B%0A%7D%0A%0Ah2%20%7B%0A%20%20border%2Dbottom%3A%204px%20solid%20%23f7f7f7%3B%0A%20%20padding%2Dtop%3A%2010px%3B%0A%20%20padding%2Dbottom%3A%202px%3B%0A%20%20font%2Dsize%3A%20145%25%3B%0A%7D%0A%0Ah3%20%7B%0A%20%20border%2Dbottom%3A%202px%20solid%20%23f7f7f7%3B%0A%20%20padding%2Dtop%3A%2010px%3B%0A%20%20font%2Dsize%3A%20120%25%3B%0A%7D%0A%0Ah4%20%7B%0A%20%20border%2Dbottom%3A%201px%20solid%20%23f7f7f7%3B%0A%20%20margin%2Dleft%3A%208px%3B%0A%20%20font%2Dsize%3A%20105%25%3B%0A%7D%0A%0Ah5%2C%20h6%20%7B%0A%20%20border%2Dbottom%3A%201px%20solid%20%23ccc%3B%0A%20%20font%2Dsize%3A%20105%25%3B%0A%7D%0A%0Aa%20%7B%0A%20%20color%3A%20%230033dd%3B%0A%20%20text%2Ddecoration%3A%20none%3B%0A%7D%0A%20%20a%3Ahover%20%7B%0A%20%20%20%20color%3A%20%236666ff%3B%20%7D%0A%20%20a%3Avisited%20%7B%0A%20%20%20%20color%3A%20%23800080%3B%20%7D%0A%20%20a%3Avisited%3Ahover%20%7B%0A%20%20%20%20color%3A%20%23BB00BB%3B%20%7D%0A%20%20a%5Bhref%5E%3D%22http%3A%22%5D%20%7B%0A%20%20%20%20text%2Ddecoration%3A%20underline%3B%20%7D%0A%20%20a%5Bhref%5E%3D%22https%3A%22%5D%20%7B%0A%20%20%20%20text%2Ddecoration%3A%20underline%3B%20%7D%0A%0A%2F%2A%20Class%20described%20in%20https%3A%2F%2Fbenjeffrey%2Ecom%2Fposts%2Fpandoc%2Dsyntax%2Dhighlighting%2Dcss%0A%20%20%20Colours%20from%20https%3A%2F%2Fgist%2Egithub%2Ecom%2Frobsimmons%2F1172277%20%2A%2F%0A%0Acode%20%3E%20span%2Ekw%20%7B%20color%3A%20%23555%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%2F%2A%20Keyword%20%2A%2F%0Acode%20%3E%20span%2Edt%20%7B%20color%3A%20%23902000%3B%20%7D%20%2F%2A%20DataType%20%2A%2F%0Acode%20%3E%20span%2Edv%20%7B%20color%3A%20%2340a070%3B%20%7D%20%2F%2A%20DecVal%20%28decimal%20values%29%20%2A%2F%0Acode%20%3E%20span%2Ebn%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20BaseN%20%2A%2F%0Acode%20%3E%20span%2Efl%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20Float%20%2A%2F%0Acode%20%3E%20span%2Ech%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20Char%20%2A%2F%0Acode%20%3E%20span%2Est%20%7B%20color%3A%20%23d14%3B%20%7D%20%2F%2A%20String%20%2A%2F%0Acode%20%3E%20span%2Eco%20%7B%20color%3A%20%23888888%3B%20font%2Dstyle%3A%20italic%3B%20%7D%20%2F%2A%20Comment%20%2A%2F%0Acode%20%3E%20span%2Eot%20%7B%20color%3A%20%23007020%3B%20%7D%20%2F%2A%20OtherToken%20%2A%2F%0Acode%20%3E%20span%2Eal%20%7B%20color%3A%20%23ff0000%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%2F%2A%20AlertToken%20%2A%2F%0Acode%20%3E%20span%2Efu%20%7B%20color%3A%20%23900%3B%20font%2Dweight%3A%20bold%3B%20%7D%20%2F%2A%20Function%20calls%20%2A%2F%20%0Acode%20%3E%20span%2Eer%20%7B%20color%3A%20%23a61717%3B%20background%2Dcolor%3A%20%23e3d2d2%3B%20%7D%20%2F%2A%20ErrorTok%20%2A%2F%0A%0A" rel="stylesheet" type="text/css" />

</head>

<body>



<div id="header">
<h1 class="title">Collect SNOTEL SWE from Niwot Ridge and compare directly to model output</h1>
<h4 class="author"><em>Logan Karsten</em></h4>
<h4 class="date"><em>2017-05-01</em></h4>
</div>


<div id="background" class="section level1">
<h1>Background</h1>
<p>Pull SNOTEL SWE at Niwot Ridge, which is located inside the Fourmile Creek test domain. This vignette also reads in SWE from the model output using the coordinates from the SNOTEL metadata. Plots comparing the data are generated for the user.</p>
</div>
<div id="setup" class="section level1">
<h1>Setup</h1>
<p>Load the rwrfhydro package.</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(<span class="st">&quot;rwrfhydro&quot;</span>)</code></pre>
<pre><code>## To check rwrfhydro updates run: CheckForUpdates()</code></pre>
<p>Load ggplo2 for plotting puposes</p>
<pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)</code></pre>
<p>Establish paths to the model output and geoGrid file for the test case.</p>
<pre class="sourceCode r"><code class="sourceCode r">geoFile &lt;-<span class="st"> '~/wrfHydroTestCases/Fourmile_Creek_testcase_v2.0/DOMAIN/geo_em_d01.Fourmile1km.nlcd11.nc'</span>
modelDir &lt;-<span class="st"> '~/wrfHydroTestCases/Fourmile_Creek_testcase_v2.0/run.FullRouting'</span></code></pre>
<p>Establish the SNOTEL site of interest, which is Niwot Ridge in this case.</p>
<pre class="sourceCode r"><code class="sourceCode r">stationMeta &lt;-<span class="st"> </span><span class="kw">subset</span>(snotelMeta,site_name ==<span class="st"> &quot;Niwot &quot;</span>)</code></pre>
</div>
<div id="data-extraction" class="section level1">
<h1>Data Extraction</h1>
<p>Obtain the domain I/J coordinates using the SNOTEL lat/lon and the GetGeogridIndex function contained within rwrfhydro.</p>
<pre class="sourceCode r"><code class="sourceCode r">snoIJ &lt;-<span class="st"> </span><span class="kw">GetGeogridIndex</span>(<span class="kw">data.frame</span>(<span class="dt">lon=</span>stationMeta$lon,<span class="dt">lat=</span>stationMeta$lat),geoFile)</code></pre>
<pre><code>## Loading required package: rgdal</code></pre>
<pre><code>## Loading required package: sp</code></pre>
<pre><code>## rgdal: version: 0.9-1, (SVN revision 518)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 1.10.1, released 2013/08/26
## Path to GDAL shared files: /usr/share/gdal/1.10
## Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 480]
## Path to PROJ.4 shared files: (autodetected)</code></pre>
<pre><code>## Loading required package: raster</code></pre>
<pre><code>## Loading required package: ncdf4</code></pre>
<p>Pull all SNOTEL observations for the given site for the 2012/2013 years.</p>
<pre class="sourceCode r"><code class="sourceCode r">snoObs &lt;-<span class="st"> </span><span class="kw">GetSnotel</span>(stationMeta$site_id,<span class="dt">series=</span><span class="st">&quot;Daily&quot;</span>,<span class="dt">startYr=</span><span class="dv">2012</span>,<span class="dt">endYr=</span><span class="dv">2013</span>)</code></pre>
<p>Pull modeled SWE at the SNOTEL point from the model directory. For a detailed explanation on the method of model extraction, see the GetMultiNcdf vignette for further explanation. Data is pulled from the pixel cell corresponding to the SNOTEL site.</p>
<pre class="sourceCode r"><code class="sourceCode r">lsmFiles &lt;-<span class="st"> </span><span class="kw">list.files</span>(<span class="dt">path=</span>modelDir,<span class="dt">pattern=</span><span class="st">'LDASOUT_DOMAIN'</span>,<span class="dt">full.names=</span><span class="ot">TRUE</span>)
fList &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dt">lsm=</span>lsmFiles)
lsmVars &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dt">SWE=</span><span class="st">'SNEQV'</span>)
varList &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dt">lsm=</span>lsmVars)
lsmInds &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dt">SWE=</span><span class="kw">list</span>(<span class="dt">start=</span><span class="kw">c</span>(snoIJ$we,snoIJ$sn,<span class="dv">1</span>),<span class="dt">end=</span><span class="kw">c</span>(snoIJ$we,snoIJ$sn,<span class="dv">1</span>),<span class="dt">stat=</span><span class="st">'mean'</span>))
indList &lt;-<span class="st"> </span><span class="kw">list</span>(<span class="dt">lsm=</span>lsmInds)
fileData &lt;-<span class="st"> </span><span class="kw">GetMultiNcdf</span>(<span class="dt">file=</span>fList,<span class="dt">var=</span>varList,<span class="dt">ind=</span>indList,<span class="dt">parallel=</span><span class="ot">FALSE</span>)</code></pre>
<p>Next, subset observations based on the date range that is contained within the modeled data frame. We will then append the subsetted obserations into the data frame in preparation for plotting.</p>
<pre class="sourceCode r"><code class="sourceCode r">fileData$obsSWE &lt;-<span class="st"> </span><span class="ot">NA</span>
for (step in <span class="dv">1</span>:<span class="kw">length</span>(fileData$value)){
   modPOSIX &lt;-<span class="st"> </span>fileData$POSIXct[step]
   ind &lt;-<span class="st"> </span><span class="kw">which</span>(snoObs$Date ==<span class="st"> </span><span class="kw">as.Date</span>(modPOSIX))
   if (<span class="kw">length</span>(ind) !=<span class="st"> </span><span class="dv">0</span>){
      fileData$obsSWE[step] &lt;-<span class="st"> </span>snoObs$SWE_mm[ind[<span class="dv">1</span>]]
   }
}</code></pre>
<p>Finally, plot the modeled values agains the observed SWE for this SNOTEL point using the ggplo2 package.</p>
<pre class="sourceCode r"><code class="sourceCode r">plotTitle &lt;-<span class="st"> </span><span class="kw">paste0</span>(<span class="st">'Fourmile Creek Modeled SWE Against Observations for: '</span>,stationMeta$site_name[<span class="dv">1</span>])
<span class="kw">ggplot</span>(fileData,<span class="kw">aes</span>(<span class="dt">x=</span>POSIXct,<span class="dt">y=</span>value,<span class="dt">color=</span><span class="st">'Simulated'</span>)) +<span class="st"> </span><span class="kw">geom_line</span>() +<span class="st"> </span>
<span class="st">  </span><span class="kw">geom_line</span>(<span class="dt">data=</span>fileData, <span class="kw">aes</span>(<span class="dt">x=</span>POSIXct,<span class="dt">y=</span>obsSWE,<span class="dt">color=</span><span class="st">'Observed'</span>),<span class="dt">size=</span><span class="fl">1.2</span>,<span class="dt">linetype=</span><span class="st">'dashed'</span>) +
<span class="st">  </span><span class="kw">scale_color_manual</span>(<span class="dt">name=</span><span class="st">'Legend'</span>,<span class="dt">values=</span><span class="kw">c</span>(<span class="st">'black'</span>,<span class="st">'red'</span>)) +<span class="st"> </span>
<span class="st">  </span><span class="kw">ggtitle</span>(plotTitle) +<span class="st"> </span><span class="kw">xlab</span>(<span class="st">'Date'</span>) +<span class="st"> </span><span class="kw">ylab</span>(<span class="st">'SWE (mm)'</span>)</code></pre>
<p><img src="" /></p>
</div>



<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
  (function () {
    var script = document.createElement("script");
    script.type = "text/javascript";
    script.src  = "https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
    document.getElementsByTagName("head")[0].appendChild(script);
  })();
</script>

</body>
</html>
